<?php

namespace App\Traits;

use Carbon\Carbon;

trait HasTimestamps
{
    /**
     * 获取创建时间格式化
     */
    public function getCreatedAtFormattedAttribute(): string
    {
        return $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : '';
    }

    /**
     * 获取更新时间格式化
     */
    public function getUpdatedAtFormattedAttribute(): string
    {
        return $this->updated_at ? $this->updated_at->format('Y-m-d H:i:s') : '';
    }

    /**
     * 获取创建时间相对时间
     */
    public function getCreatedAtHumanAttribute(): string
    {
        return $this->created_at ? $this->created_at->diffForHumans() : '';
    }

    /**
     * 获取更新时间相对时间
     */
    public function getUpdatedAtHumanAttribute(): string
    {
        return $this->updated_at ? $this->updated_at->diffForHumans() : '';
    }

    /**
     * 检查是否为今天创建
     */
    public function isCreatedToday(): bool
    {
        return $this->created_at && $this->created_at->isToday();
    }

    /**
     * 检查是否为昨天创建
     */
    public function isCreatedYesterday(): bool
    {
        return $this->created_at && $this->created_at->isYesterday();
    }

    /**
     * 检查是否为本周创建
     */
    public function isCreatedThisWeek(): bool
    {
        return $this->created_at && $this->created_at->isCurrentWeek();
    }

    /**
     * 检查是否为本月创建
     */
    public function isCreatedThisMonth(): bool
    {
        return $this->created_at && $this->created_at->isCurrentMonth();
    }

    /**
     * 检查是否为本年创建
     */
    public function isCreatedThisYear(): bool
    {
        return $this->created_at && $this->created_at->isCurrentYear();
    }

    /**
     * 获取创建时间距离现在的天数
     */
    public function getCreatedDaysAgoAttribute(): int
    {
        return $this->created_at ? $this->created_at->diffInDays(now()) : 0;
    }

    /**
     * 获取更新时间距离现在的天数
     */
    public function getUpdatedDaysAgoAttribute(): int
    {
        return $this->updated_at ? $this->updated_at->diffInDays(now()) : 0;
    }

    /**
     * 获取创建时间距离现在的小时数
     */
    public function getCreatedHoursAgoAttribute(): int
    {
        return $this->created_at ? $this->created_at->diffInHours(now()) : 0;
    }

    /**
     * 获取更新时间距离现在的小时数
     */
    public function getUpdatedHoursAgoAttribute(): int
    {
        return $this->updated_at ? $this->updated_at->diffInHours(now()) : 0;
    }

    /**
     * 获取创建时间距离现在的分钟数
     */
    public function getCreatedMinutesAgoAttribute(): int
    {
        return $this->created_at ? $this->created_at->diffInMinutes(now()) : 0;
    }

    /**
     * 获取更新时间距离现在的分钟数
     */
    public function getUpdatedMinutesAgoAttribute(): int
    {
        return $this->updated_at ? $this->updated_at->diffInMinutes(now()) : 0;
    }

    /**
     * 检查是否在指定时间内创建
     */
    public function isCreatedWithin(Carbon $time): bool
    {
        return $this->created_at && $this->created_at->gte($time);
    }

    /**
     * 检查是否在指定时间内更新
     */
    public function isUpdatedWithin(Carbon $time): bool
    {
        return $this->updated_at && $this->updated_at->gte($time);
    }

    /**
     * 获取创建时间范围
     */
    public function scopeCreatedBetween($query, Carbon $start, Carbon $end)
    {
        return $query->whereBetween('created_at', [$start, $end]);
    }

    /**
     * 获取更新时间范围
     */
    public function scopeUpdatedBetween($query, Carbon $start, Carbon $end)
    {
        return $query->whereBetween('updated_at', [$start, $end]);
    }

    /**
     * 获取今天创建的记录
     */
    public function scopeCreatedToday($query)
    {
        return $query->whereDate('created_at', today());
    }

    /**
     * 获取昨天创建的记录
     */
    public function scopeCreatedYesterday($query)
    {
        return $query->whereDate('created_at', yesterday());
    }

    /**
     * 获取本周创建的记录
     */
    public function scopeCreatedThisWeek($query)
    {
        return $query->whereBetween('created_at', [now()->startOfWeek(), now()->endOfWeek()]);
    }

    /**
     * 获取本月创建的记录
     */
    public function scopeCreatedThisMonth($query)
    {
        return $query->whereMonth('created_at', now()->month)
                    ->whereYear('created_at', now()->year);
    }

    /**
     * 获取本年创建的记录
     */
    public function scopeCreatedThisYear($query)
    {
        return $query->whereYear('created_at', now()->year);
    }

    /**
     * 获取最近N天创建的记录
     */
    public function scopeCreatedInDays($query, int $days)
    {
        return $query->where('created_at', '>=', now()->subDays($days));
    }

    /**
     * 获取最近N小时创建的记录
     */
    public function scopeCreatedInHours($query, int $hours)
    {
        return $query->where('created_at', '>=', now()->subHours($hours));
    }

    /**
     * 获取最近N分钟创建的记录
     */
    public function scopeCreatedInMinutes($query, int $minutes)
    {
        return $query->where('created_at', '>=', now()->subMinutes($minutes));
    }

    /**
     * 按创建时间排序
     */
    public function scopeOrderByCreatedAt($query, string $direction = 'desc')
    {
        return $query->orderBy('created_at', $direction);
    }

    /**
     * 按更新时间排序
     */
    public function scopeOrderByUpdatedAt($query, string $direction = 'desc')
    {
        return $query->orderBy('updated_at', $direction);
    }

    /**
     * 获取创建时间戳
     */
    public function getCreatedTimestampAttribute(): int
    {
        return $this->created_at ? $this->created_at->timestamp : 0;
    }

    /**
     * 获取更新时间戳
     */
    public function getUpdatedTimestampAttribute(): int
    {
        return $this->updated_at ? $this->updated_at->timestamp : 0;
    }
}
